/* Copyright (C) 2014-2016 Dan Chapman <dpniel@ubuntu.com>

   This file is part of Dekko email client for Ubuntu Devices/

   This program is free software; you can redistribute it and/or
   modify it under the terms of the GNU General Public License as
   published by the Free Software Foundation; either version 2 of
   the License or (at your option) version 3

   This program is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   GNU General Public License for more details.

   You should have received a copy of the GNU General Public License
   along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/
#ifndef NS_NOTIFICATIONSERVICE_H
#define NS_NOTIFICATIONSERVICE_H

#include "PostalServiceWorker.h"
#include <QObject>
#include <QTimer>
#include <QList>
#include "app/Accounts/AccountsManager.h"
#include "Launcher/EmblemCounter.h"
#include "postalmessage.h"

namespace Dekko
{
namespace Accounts
{
class AccountsManager;
}
namespace Notifications
{
class NotificationWorker;
class PostalServiceWorker;
// The NotificationgService has special priviliges in that it can open/close
// And adjust the accounts imap model based on the applications state.
// So when the application is no longer active it will start a process that
// disconnects the imapModel socket and connects the notificationModel to the relevent
// slots, Mail arrivals will turn into notifications, errors where warrented such as SSL certificate changed
// and it's untrusted or some other heuristic will also be notifications in the messaging menu. Not sure it needs a popup ??
class NotificationService : public QObject
{
    Q_OBJECT
//    QML_WRITABLE_PROPERTY(bool, useBackgroundThread)
    typedef QList<NotificationWorker *> MailboxWorkers;
public:
    explicit NotificationService(QObject *parent = 0);
//    ~NotificationService();

    Q_INVOKABLE void setManager(QObject *manager);
//    // Start the NotificationWorker in a new thread
//    // Note: this will not open the connections
//    // use NotificationService::openConnections to start the service
//    Q_INVOKABLE void start();
//    // Stop the NotificationWorker
//    // NOTE: this will quit & delete the worker thread.
//    Q_INVOKABLE void stop();
//    // Determine if NotificationWorker is running
//    Q_INVOKABLE bool isRunning() const;

signals:
//    // start the service
//    void openConnections();
//    // sets the service network policy
//    // So it would be a good idea to set this offline
//    // whe Qt.application.active. If the app is on screen
//    // and the phone is awake then we don't need to be watching
//    void setNetworkPolicy(const bool online);
//    // Disable but don't delete the service
//    void closeConnections();
//    // This means settings were changed in the IMAP settings
//    // from in the app so we need to reload our configuration
//    // in the worker.
//    void reloadConnections();

//    void tokenChanged(QString token);

//    void updateUnreadCount(int count);
    void updateStartTime(QDateTime time);
public slots:
    void updateUnreadCount(const int &count);

private slots:
    void sendNotification(QSharedPointer<PostalMessage> notification);
//    void logMessage(QString &message);
    void handleAppStateChanged(const Qt::ApplicationState &state);
    void startNotificationService();
    void stopNotificationService();
    void handleImapError(const QString &error);
    void handleAlert(const QString &alert);

    void handleAuthAttemptFailed(const QString &authError);
////            connect(account,
////                    SIGNAL(checkSslPolicy()),
////                    this,
////                    SLOT(showCheckSslPolicy()));

private:
    enum ImapModelState {ONLINE, OFFLINE};
    void setImapModelConnectionState(const ImapModelState state);
    void connectNotificationModels();
    void disconnectNotificationModels();
    void sendErrorMessage(Accounts::Account *account, const QString &message, const QString &icon);

    QPointer<Accounts::AccountsManager> m_manager;
    QPointer<Launcher::EmblemCounter> m_emblemCounter;
    QTimer *m_timer;
//    NotificationWorker m_worker;
//    QThread m_workerThread;
    PostalServiceWorker *m_postalService;
    MailboxWorkers m_workers;
    bool m_running;
};
}
}
#endif // NS_NOTIFICATIONSERVICE_H
